// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.common.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liuxinlong.modules.common.controller.request.BaseReq;
import com.liuxinlong.modules.common.controller.request.QueryComprehensiveReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.liuxinlong.common.TreeNode;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryDashboardReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.common.service.DashboardService;

/**
 * 首页控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月26日
 */
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 综合检索
     *
     * @return 综合检索
     */
    @PostMapping("query")
    public BaseResp queryComprehensive(@RequestBody QueryComprehensiveReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("keyword", req.getKeyword());
        queryParam.put("label", req.getLabel());
        queryParam.put("range", req.getRange());
        queryParam.put("product", req.getProduct());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        Map<String,Object> dataMap = dashboardService.queryComprehensive(queryParam);
        int total = (int) dataMap.get("totalNum");
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setTotalNum(total);
        resp.setRetMessage("综合检索成功");
        return resp;
    }

    /**
     * 查询知识树
     *
     * @return 知识树信息
     */
    @PostMapping("tree-query")
    public BaseResp queryKnowledgeTree() {

        TreeNode resulTreeNode = dashboardService.queryKnowledgeTree();
        Map<String, Object> map = new HashMap<>();
        map.put("data", resulTreeNode);
        BaseResp resp = new BaseResp();
        resp.setDataMap(map);
        resp.setRetMessage("查询知识树成功");
        return resp;
    }

    /**
     * 查询知识图谱
     *
     * @return 知识图谱信息
     */
    @PostMapping("graph")
    public BaseResp queryKnowledgeGraph(@RequestBody QueryDashboardReq req) {

        TreeNode resulTreeNode = dashboardService.queryKnowledgeGraph(req.getRootId(), req.getDeep());
        Map<String, Object> map = new HashMap<>();
        map.put("data", resulTreeNode);
        BaseResp resp = new BaseResp();
        resp.setDataMap(map);
        resp.setRetMessage("查询知识图谱成功");
        return resp;
    }

    /**
     * 查询节点详细信息
     *
     * @return 节点详细信息
     */
    @PostMapping("detail")
    public BaseResp queryRootDetail(@RequestBody BaseDeleteReq req) {
        Map<String, Object> map = dashboardService.queryRootDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(map);
        resp.setRetMessage("查询知识图谱成功");
        return resp;
    }

    /**
     * 查询收录标准数
     *
     * @return 收录标准数
     */
    @GetMapping("total")
    public BaseResp queryStandardTotal() {
        Map<String, Object> queryParam = new HashMap<>();
        int total = dashboardService.countComprehensive(queryParam);
        BaseResp resp = new BaseResp();
        resp.setTotalNum(total);
        resp.setRetMessage("查询收录标准数成功");
        return resp;
    }

    /**
     * 查询轮播、热搜、推荐信息
     *
     * @return 轮播信息
     */
    @GetMapping("rotation")
    public BaseResp queryNewsRotation() {
        Map<String, Object> dataMap = dashboardService.queryNewsRotation();
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查询首页信息成功");
        return resp;
    }

    /**
     * 查询标准动态
     *
     * @return 标准动态
     */
    @PostMapping("news")
    public BaseResp queryStandardNews(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum",req.getStartNum());
        queryParam.put("pageSize",req.getPageSize());
        List<Map<String, Object>> dataList = dashboardService.queryStandardNews(queryParam);
        int totalNum = dashboardService.countStandardNews(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询标准动态成功");
        return resp;
    }
}
